
const BASE_DEVICE_WIDTH = 750;
const deviceDPR = 1;
const isIOS = true;
const deviceWidth = 750;

function transformRPX(number, newDeviceWidth) {
    if (number === 0) return 0;
    number = number / BASE_DEVICE_WIDTH * (newDeviceWidth || deviceWidth);
    number = Math.floor(number + eps);
    if (number === 0) {
        if (deviceDPR === 1 || !isIOS) {
            return 1;
        } else {
            return 0.5;
        }
    }
    return number;
}

var setCssToHead = function (file, _xcInvalid) {
    var Ca = {};	//最终的css key/value对
    var css_id;
    var _C = [];	//文件索引列表

    function makeup(file, opt) {
        // 如果file是数字类型
        var isNumber = typeof (file) === "number";
        //
        if (isNumber && Ca.hasOwnProperty(file)) return ""; //file已存在，直接返回
        if (isNumber) Ca[file] = 1;	//file不存在做一个标记
        var ex = isNumber ? _C[file] : file;	//如果是数字类型，从_C中获取文件名，否则直接使用file，file是个数组
        var res = "";

        //ex也是一个array
        for (var i = ex.length - 1; i >= 0; i--) {
            var content = ex[i];	//".", [1],
            if (typeof (content) === "object") {    //这里其实就是判断是否是一个array类型
                var op = content[0];
                if (op == 0)
                    res = transformRPX(content[1], opt.deviceWidth) + "px" + res;	//把content[1]转成px
                else if (op == 1)
                    res = opt.suffix + res;	//直接把后缀加上
                else if (op == 2)
                    res = makeup(content[1], opt) + res;	//允许嵌套，但是为什么需要嵌套？css好像没有嵌套流程？好吧，应该可以嵌套
            } else
                res = content + res	//content放最前res所以拼到最后
        }
        return res;
    }
    var rewritor = function (suffix, opt, style) {  //默认情况下suffix是空
        opt = opt || {};
        suffix = suffix || "";
        opt.suffix = suffix;
        if (opt.allowIllegalSelector != undefined && _xcInvalid != undefined) {
            if (opt.allowIllegalSelector)
                console.warn("For developer:" + _xcInvalid);
            else {
                console.error(_xcInvalid + "This wxss file is ignored.");
                return;
            }
        }
        Ca = {};
        css = makeup(file, opt);
        if (!style) {
            var head = document.head || document.getElementsByTagName('head')[0];
            window.__rpxRecalculatingFuncs__ = window.__rpxRecalculatingFuncs__ || [];
            style = document.createElement('style');
            style.type = 'text/css';
            head.appendChild(style);
            window.__rpxRecalculatingFuncs__.push(function (size) {
                opt.deviceWidth = size.width;
                rewritor(suffix, opt, style);
            });
        }
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            if (style.childNodes.length == 0)
                style.appendChild(document.createTextNode(css));
            else
                style.childNodes[0].nodeValue = css;
        }
    }
    return rewritor;
};

(function() {
    setCssToHead([])();
    setCssToHead([".", [1],
        "container { height: 100%; display: -webkit-flex; display: flex; -webkit-flex-direction: column; flex-direction: column; -webkit-align-items: center; align-items: center; -webkit-justify-content: space-between; justify-content: space-between; padding: ", [
            0, 200
        ], " 0; box-sizing: border-box; }\n",
    ])();
}());
